
class JSTableBrowser < Pan::Component
  attr_accessor :element_id
  def initialize(provider)
    @provider = provider
    @element_id = 'a-table'
  end
  
  def get_meta_data
    @rows = @provider.rows()
    @cols = @provider.cols()
    @col_headings = @provider.headings()
  end

  def render_on(html)
    get_meta_data()
    render_js_on(html)
    # html.heading("#{@provider.class}",3)
    html.table("id"=>element_id,"border"=>"0","cellspacing"=>"0","class"=>"clear-both") {
      html.thead {
        html.tr('class'=>"header") {
          @provider.headings.each { |heading|
            html.th { 
              html.text(heading)
            }
        }
        }
      }
      html.tbody {
        render_rows_on(html)
      }
    }
  end

  def render_rows_on(html)
    (0..@rows-1).each do |row_number|
      html.tr {
        @provider.headings.each_with_index do |colname, col_number|
          html.td("class"=>row_number%2==0?"":"alt") {
            @provider.place_content(html,row_number,col_number,colname)
          }
        end
      }
    end
  end

  def render_js_on(html)
    html.script {
      html.text(<<EOS
$(document).ready(function() { 
        $("##{element_id}")
          .tablesorter()
          .addClass("tablesorter");
    } 
); 
EOS
                )}
  end
end
